package comp.wxapi.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.github.binarywang.wxpay.config.WxPayConfig;

import comp.wxapi.bean.BaseConfigBean;
import comp.wxapi.constants.Constants;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;

@Service
public class MpService extends WxAbstractBaseService {
	
	@Override
	public BaseConfigBean getBaseConfig() {
		printAllHeaders();
		return getBaseConfig(Constants.MP_KEY, null);
	}
	
	public BaseConfigBean getBaseConfig(String group) {
		printAllHeaders();
		if(StringUtils.isEmpty(group)) {
			group = "default";
		}
		return getBaseConfig(Constants.MP_KEY, group);
	}
	
	@Override
	public WxPayConfig getPayConfig(boolean isRefund) {
		printAllHeaders();
		return super.getPayConfig(isRefund,Constants.MP_KEY,Constants.DEFAULT);
	}

	@Override
	public WxMpService wxMpService(BaseConfigBean baseConfigBean) {
		String appid = baseConfigBean.getAppid();
		WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
		wxMpConfigStorage.setAppId(appid);
		wxMpConfigStorage.setSecret(baseConfigBean.getAppsecret());
		wxMpConfigStorage.updateAccessToken(getAccessToken(appid, baseConfigBean.getAppsecret()), 7200);
		WxMpService wxMpService = new WxMpServiceImpl();
		wxMpService.setWxMpConfigStorage(wxMpConfigStorage);
		return wxMpService;
	}

}
